<?php
$xml = simplexml_load_file('data.xml');

$output = "";

$output .= "<?xml version='1.0'?><rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#' xmlns:country='http://www.127.0.0.1/country#'>";

//continent part
foreach($xml->country as $country)
{
	$country_name 				= $country["name"];
	$country_id 				= $country["id"];
	$country_capital 			= $country["capital"];
	$country_population 		= $country["population"];
	$country_datacode 			= $country["datacode"];
	$country_totalarea 			= $country["total_area"];
	$country_population_growth 	= $country["population_growth"];
	$country_infant_mortality 	= $country["infant_mortality"];
	$country_gdp_agri 			= $country["gdp_agri"];
	$country_gdp_total 			= $country["gdp_total"];
	$country_inflation 			= $country["inflation"];
	$country_indep_date 		= $country["indep_date"];
	$country_government 		= $country["government"];
	$country_car_code 			= $country["car_code"];


	$output .= "<rdf:Description rdf:about='http://www.127.0.0.1/country/$country_name'>";
	$output .= "<country:id>$country_id</country:id>";
	$output .= "<country:name>$country_name</country:name>";
	$output .= "<country:capital>$country_capital</country:capital>";
	$output .= "<country:population>$country_population</country:population>";
	$output .= "<country:datacode>$country_datacode</country:datacode>";
	$output .= "<country:totalarea>$country_totalarea</country:totalarea>";
	$output .= "<country:population_growth>$country_population_growth</country:population_growth>";
	$output .= "<country:infant_mortality>$country_infant_mortality</country:infant_mortality>";
	$output .= "<country:gdp_agri>$country_gdp_agri</country:gdp_agri>";
	$output .= "<country:gdp_total>$country_gdp_total</country:gdp_total>";
	$output .= "<country:inflation>$country_inflation</country:inflation>";
	$output .= "<country:indep_date>$country_indep_date</country:indep_date>";
	$output .= "<country:government>$country_government</country:government>";
	$output .= "<country:car_code>$country_car_code</country:car_code>";
	
	$output .= "<country:religion><rdf:Seq>";
	
	$rel_count = 0;
	
	
	foreach($country->religions as $religion)
	{

		$rel_percentage = $religion["percentage"];
		$rel_name = $country->{'religions'}[$rel_count];
			
		$output .= "<rdf:li>$rel_name: $rel_percentage %</rdf:li>";
		$rel_count++;
	}
	
	$output .= "</rdf:Seq></country:religion>";
	
	$output .= "</rdf:Description>";
}

$output .= "</rdf:RDF>";


$myfile = fopen("output.rdf", "w") or die("Unable to open file!");
fwrite($myfile, $output);
fclose($myfile);

echo "file written successfully!";
?>